﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace klcc
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            UserSqlDataSource.SelectCommand += String.Format(" WHERE Username='{0}' AND Password='{1}' AND Locked='0'", 
                UsernameTxt.Text, 
                PasswordTxt.Text);

            DataView dv = (DataView)UserSqlDataSource.Select(DataSourceSelectArguments.Empty);

            //Response.Write(dv.Count);

            if (dv.Count > 0)
            {
                Session["UserId"] = dv.Table.Rows[0]["UserId"];
                Session["Username"] = dv.Table.Rows[0]["Username"];
                //Redirect to index (Still need to select Company and Project);
                Response.Redirect("~/");
            }
            else
            {
                MsgLB.Text = String.Format("帳號 '{0}' 無法登入系統，帳號(密碼)錯誤？", UsernameTxt.Text);
            }
        }
    }
}
